﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DevExpress.Web.Mvc;
using Webcar.BLL;
using Webcar.Entitys;
using Webcar.Utils;
using System.Data.Entity;
using System.Web.UI.WebControls;
using System.Threading;
using System.Web.UI;
using EntityFramework.Extensions;
namespace Webcar.Controllers
{
    public class PhotoController : BaseControler
    {
        public ActionResult Delete(string photo_type, string id,string pkid)
        {
            
            if (photo_type == "Car")
            {
                CurrentUser.EnsurePermission(PermissionManager.编辑车辆档案);
                WebcarDataProvider.DB.BS_CAR_PHOTO.Delete(a => a.URL == id);
            }
            if (photo_type == "Driver")
            {
                CurrentUser.EnsurePermission(PermissionManager.编辑驾驶员);
                WebcarDataProvider.DB.BS_EMP_PHOTO.Delete(a => a.URL == id);
            }
            if (photo_type == "Sec")
            {
                CurrentUser.EnsurePermission(PermissionManager.编辑安全事故);
                WebcarDataProvider.DB.SEC_PHOTO.Delete(a => a.URL == id);
            }
            return RedirectToAction("List", "Photo", new { photo_type = photo_type, id = pkid });
        }
        public ActionResult List(string photo_type,string id)
        {
            if (photo_type == "Car")
            {
                var list = from p in WebcarDataProvider.DB.BS_CAR_PHOTO
                           where p.CAR_NO == id
                           orderby p.XH
                           select new { PhotoUrl= p.URL, Name = p.PHOTO_NAME };
                return View(model: list.ToList());
            }
            if (photo_type == "Driver")
            {
                var list = from p in WebcarDataProvider.DB.BS_EMP_PHOTO
                           where p.MAN_CODE == id
                           orderby p.XH
                           select new { PhotoUrl = p.URL, Name = p.REMARK };
                return View(model: list.ToList());
            }
            if (photo_type == "Sec")
            {
                var list = from p in WebcarDataProvider.DB.SEC_PHOTO
                           where p.SEC_NO == id
                           orderby p.XH
                           select new { PhotoUrl = p.URL, Name = p.REMARK };
                return View(model: list.ToList());
            }
            
            return View();
        }
        public ActionResult Save(string id,string PhotoName,string photo_type)
        {
            string filename = Request.Files.UploadImage(photo_type);
            if (filename == null || id == null)
            {
                throw new Exception("上传参数错误。");
            }
            using (var DB = new WebcarContext())
            {
                if (photo_type == "Car")
                {
                    CurrentUser.EnsurePermission(PermissionManager.编辑车辆档案);
                    BS_CAR_PHOTO photo = new BS_CAR_PHOTO();
                    photo.CAR_NO = id;
                    photo.URL = filename;
                    photo.PHOTO_NAME = PhotoName;
                    photo.FILEEXT = "ext";
                    photo.XH = DB.BS_CAR_PHOTO.Where(a => a.CAR_NO == id).MaxOrDefault(a => a.XH) + 1;
                    DB.BS_CAR_PHOTO.Add(photo);
                    DB.SaveChanges();
                }
                if (photo_type == "Driver")
                {
                    CurrentUser.EnsurePermission(PermissionManager.编辑驾驶员);
                    BS_EMP_PHOTO photo = new BS_EMP_PHOTO();
                    photo.MAN_CODE = id;
                    photo.URL = filename;
                    photo.REMARK = PhotoName;
                    photo.FILEEXT = "ext";
                    photo.XH = DB.BS_EMP_PHOTO.Where(a => a.MAN_CODE == id).MaxOrDefault(a => a.XH) + 1;
                    DB.BS_EMP_PHOTO.Add(photo);
                    DB.SaveChanges();
                }
                if (photo_type == "Sec")
                {
                    CurrentUser.EnsurePermission(PermissionManager.编辑安全事故);
                    SEC_PHOTO photo = new SEC_PHOTO();
                    photo.SEC_NO = id;
                    photo.URL = filename;
                    photo.REMARK = PhotoName;
                    photo.FILEEXT = "ext";
                    photo.XH = DB.SEC_PHOTO.Where(a => a.SEC_NO == id).MaxOrDefault(a => a.XH) + 1;
                    DB.SEC_PHOTO.Add(photo);
                    DB.SaveChanges();
                }
            }
            return RedirectToAction("List", "Photo", new { photo_type = photo_type, id = id });
            //throw new Exception("参数错误。");
        }
        public ActionResult ShowSmall(string photo_type,string id)
        {
            // 检查请求下载文件的有效性
            string pfn = Server.MapPath("~/photo/" + photo_type+"/small/" + id);
            //if (!System.IO.File.Exists(pfn))
            //{
            //    throw new ArgumentException("无效文件，文件不存在!");
            //}
            // 字节流返回文件结果给视图
            return new FilePathResult(pfn,FileUploadHelper.GetContentType(pfn));
        }
        public ActionResult ShowLarge(string photo_type, string id)
        {
            // 检查请求下载文件的有效性
            string pfn = Server.MapPath("~/photo/" + photo_type + "/large/" + id);
            //if (!System.IO.File.Exists(pfn))
            //{
            //    throw new ArgumentException("无效文件，文件不存在!");
            //}
            // 字节流返回文件结果给视图
            return new FilePathResult(pfn, FileUploadHelper.GetContentType(pfn));
        }
    }
    

}